package com.lopo.springSecurity.login.jwt;

import cn.hutool.jwt.JWTUtil;
import com.lopo.springSecurity.config.BaseAuthenticationProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class JwtAuthenticationProvider extends BaseAuthenticationProvider implements AuthenticationProvider {

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String token = (String) authentication.getCredentials();

//        final boolean verify = JWTUtil.verify(token, "123456".getBytes(StandardCharsets.UTF_8));
//        if (!verify) {
//            throw new RuntimeException("登录错误");
//        }
        SecurityContextHolder.getContext().setAuthentication(authentication);
        return authentication;

    }

    @Override
    public boolean supports(Class<?> authentication) {
        return JwtAuthToken.class.isAssignableFrom(authentication);
    }
}
